import { appState } from './appManager'

/**
 * 开发环境才有保存 本地保存token
 */
export function saveCookie(name:string, value:string) {
    if (appState.embed) {
        sessionStorage.setItem(name, value)
        return
    }
    document.cookie = name + '=' + encodeURIComponent(value) + ';'
}

/**
 *
 * @param name
 */
export function getCookie(name:string) {
    if (appState.embed) return sessionStorage.getItem(name) || ''
    const arr = document.cookie.match(new RegExp('(^| )' + name + '=([^;]*)(;|$)'))
    return arr ? decodeURIComponent(arr[2]) : null
}

/**
 *
 * @param name
 */
export function delCookie(name:string) {
    if (appState.embed) {
        sessionStorage.removeItem(name)
        return
    }
    const exp:any = new Date()
    exp.setTime(exp.getTime() - 1)
    const cName = getCookie(name)
    if (cName != null) document.cookie = name + '=' + cName + ';expires=' + exp.toGMTString()
}
